package com.common.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {

    static Logger log = LoggerFactory.getLogger(DateUtil.class);

    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM_DD2 = "yyyy/MM/dd";
    public static final String YYYY_MM_DD3 = "yyyyMMdd";
    public static final String MM_DD = "MM/dd";
    public static final String FULL_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FULL_TIME2 = "yyyy/MM/dd HH:mm:ss";


    /**
     * 日期转化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String convertDateTocString(Date date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        String format = dateFormat.format(date);
        return format;
    }

    public static Date convertStringToDate(String date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        Date format = null;
        try {
            format = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("convertStringToDate -->{}",e);
        }
        return format;
    }

    /**
     * 日期转化
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String convertDateTocString(String date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        String format = dateFormat.format(date);
        return format;
    }

    public static long convertStringTocMsec(String date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        Date parse = null;
        try {
            parse = dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            log.error("convertStringTocMsec："+date,e);
        }
        long time = parse.getTime();
        return time;

    }

    public static String convertNowToString(String pattern) {
        return convertDateTocString(new Date(), pattern);
    }

    public static String convertNowToString() {
        return convertDateTocString(new Date(), FULL_TIME);
    }

    /**
     * 描述:获取下一个月.
     *
     * @return
     */
    public static Date getPreMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        return cal.getTime();
    }

    public static String getNow(){
        return getNow("yyyy-MM-dd");
    }

    public static String getNow(String formatStr){
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(new Date());
    }


    public static Date getNowDate(String formatStr){
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            return format.parse(getNow(formatStr));
        } catch (ParseException e) {
            log.error("日期转化失败", e);
            return null;
        }
    }

    public static String getDateStr(String date, String formatStr){
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            return format.format(getDate(date,formatStr));
        } catch (Exception e) {
            log.error("日期转化失败", e);
            return null;
        }
    }

    public static Date getDate(String date, String formatStr){
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            log.error("日期转化失败", e);
            return null;
        }
    }


    /**
     * @Description: 当前时间几小时后时间
     * @Param: [d 时间, day 几天前]
     * @return: java.util.Date
     * @Author: zyj
     * @Date: 2020/2/11
     */
    public static Date getNowHourAfter( int hour){
        Date date = getHourAfter(new Date(), hour);
        return date;
    }

    /**
     * @Description: 几小时后时间
     * @Param: [d 时间, day 几天前]
     * @return: java.util.Date
     * @Author: zyj
     * @Date: 2020/2/11
     */
    public static Date getHourAfter(Date d, int hour){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.HOUR,now.get(Calendar.HOUR)+hour);
        return now.getTime();
    }

    /**
     * 描述:获取N天后
     *
     * @return
     */
    public static String getAfterDay(int num) {
        return getAfterDay("yyyy-MM-dd HH:mm:ss", num);
    }

    /**
     * 描述:获取N天后
     *
     * @return
     */
    public static String getAfterDay(String formatStr, int num) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, num);
        return format.format(cal.getTime());
    }

    /**
    * @Description: 几天前时间
    * @Param: [d 时间, day 几天前]
    * @return: java.util.Date
    * @Author: zyj
    * @Date: 2020/2/11
    */
    public static Date getDateBefore(Date d, int day){
        Calendar now =Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE,now.get(Calendar.DATE)-day);
        return now.getTime();
    }

    /**
     * 得到当前时间几天前的时间
     * @param day
     * @return
     */
    public static Date getDateBefore(int day){
        return getDateBefore(new Date(), day);
    }

    /**
     * 描述:获取前N天
     *
     * @return
     */
    public static String getOldDay(int num) {
        return getOldDay("yyyy-MM-dd HH:mm:ss", num);
    }

    /**
     * 描述:获取前N天
     *
     * @return
     */
    public static String getOldDay(String formatStr, int num) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, -num);
        return format.format(cal.getTime());
    }

    /**
     * 描述:获取前N 个月
     *
     * @return
     */
    public static String getOldMonth(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.MONTH, -num);
        return format.format(cal.getTime());
    }

    /**
     * 描述:获取前N 年
     *
     * @return
     */
    public static String getOldYear(int num) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.YEAR, -num);
        return format.format(cal.getTime());
    }

    /**
     * 获取最近几月
     * @param num
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author yst
     */
    public static Map<String,String> getRecentMonth(int num) {
        Map<String,String> map = new HashMap<>(2);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        String endDate = format.format(cal.getTime());
        cal.add(Calendar.MONTH, -num);
        String beginDate = format.format(cal.getTime());
        map.put("endDate",endDate);
        map.put("beginDate",beginDate);
        return map;
    }

    /**
     * 获取最近几年
     * @param num
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author yst
     */
    public static Map<String,String> getRecentYear(int num) {
        Map<String,String> map = new HashMap<>(2);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        String endDate = format.format(cal.getTime());
        cal.add(Calendar.YEAR, -num);
        String beginDate = format.format(cal.getTime());
        map.put("endDate",endDate);
        map.put("beginDate",beginDate);
        return map;
    }

    public static String getDateStr() {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE, 0);
        return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());//20180625
    }

    public static Timestamp getTimeStamp(String format, String dateStr){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = getDate(dateStr, format);
        Timestamp timestamp = new Timestamp(date.getTime());
        return timestamp;
    }


    public static Timestamp getTimeStampNow(){
        Date date = new Date();
       return new Timestamp(date.getTime());
    }

    public static String getStringTime(Timestamp timestamp,String format) throws Exception {
        return new SimpleDateFormat(format).format(timestamp);  // 获取年月日时分秒
    }

}
